import Vue from "vue";
import Vuex from "vuex";
import {
	isEmpty
} from '@/utils/common.js'

Vue.use(Vuex);

const type = {
	SET_USER: "SET_USER", // 快递员
	SET_SITE: "SET_SITE", // 网点
	SET_TOKEN: "SET_TOKEN", // token
	SET_DEV_ID: "SET_DEV_ID", // 设备
	SET_ISLAO: "SET_ISLAO", // 语言
	SET_USERNAME: "SET_USERNAME", // 用户名   
	SET_ORDER: "SET_ORDER", // 订单
	SET_TRANSLATE: "SET_TRANSLATE", // 翻译
	EXIT_LOGIN: "EXIT_LOGIN" // 退出登录
};

const state = {
	user: {},
	site: {},
	order: {},
	token: "",
	devId: "",
	username: "",
	isLao: false,
	translate: 'google'
};

//获取例子：this.$store.getters.token
const getters = {
	user: state => state.user,
	site: state => state.site,
	order: state => state.order,
	token: state => state.token,
	devId: state => state.devId,
	isLao: state => state.isLao,
	username: state => state.username,
	translate: state => state.translate
};

const mutations = {
	[type.SET_ISLAO](state, isLao) {
		uni.setStorageSync('isLao', !!isLao)
		state.isLao = !!isLao
	},
	[type.SET_USERNAME](state, username) {
		if (username) {
			uni.setStorageSync('username', username)
			state.username = username
		} else {
			uni.setStorageSync('username', "")
			state.username = ""
		}
	},
	[type.SET_DEV_ID](state, devId) {
		if (devId) {
			uni.setStorageSync('devId', devId)
			state.devId = devId
		} else {
			uni.setStorageSync('devId', devId)
			state.devId = devId
		}
	},
	[type.SET_TOKEN](state, token) {
		if (token) {
			let data = token.indexOf('Bearer') != -1 ? token : "Bearer " + token
			uni.setStorageSync('token', data)
			state.token = data
		} else {
			uni.setStorageSync('token', '')
			state.token = ''
		}
	},
	[type.EXIT_LOGIN](state) {
		uni.setStorageSync('user', {})
		uni.setStorageSync('site', {})
		uni.setStorageSync('token', '')
		state.user = {}
		state.site = {}
		state.token = ''
	},
	[type.SET_USER](state, user) {
		if (user) state.user = user;
		else state.user = {}
	},
	[type.SET_ORDER](state, order) {
		if (order) state.order = order;
		else state.order = {}
	},
	[type.SET_SITE](state, site) {
		if (site) state.site = site;
		else state.site = {}
	},
	[type.SET_TRANSLATE](state, translate) {
		if (translate) state.translate = translate;
		else state.translate = 'google'
	}
};

//设置例子：this.$store.dispatch('setToken',value).then(response => {}).catch(error => {})
const actions = {
	setUser: ({
		commit
	}, user) => {
		commit(type.SET_USER, user);
	},
	setIsLao: ({
		commit
	}, isLao) => {
		commit(type.SET_ISLAO, isLao);
	},
	setUsername: ({
		commit
	}, username) => {
		commit(type.SET_USERNAME, username);
	},
	setDevId: ({
		commit
	}, devId) => {
		commit(type.SET_DEV_ID, devId);
	},
	setToken: ({
		commit
	}, token) => {
		commit(type.SET_TOKEN, token);
	},
	exitLogin: ({
		commit
	}) => {
		commit(type.EXIT_LOGIN);
	},
	setTranslate: ({
		commit
	}, translate) => {
		commit(type.SET_TRANSLATE, translate)
	},
	setOrder: ({
		commit
	}, order) => {
		commit(type.SET_ORDER, order)
	},
	setSite: ({
		commit
	}, site) => {
		commit(type.SET_SITE, site)
	}
};

export default new Vuex.Store({
	state,
	getters,
	mutations,
	actions
});
